/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class GttUserActivity extends Activity implements OnClickListener {
	EditText UserEdit;
	EditText PwdEdit;
	EditText BotEdit;
	EditText ScreenNameEdit;
	Button OKBtn;
	Button ImportBtn;
	String user,password,bot,screen_name;
	SharedPreferences pref;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user);
		pref = PreferenceManager.getDefaultSharedPreferences(this);
		user = pref.getString("user", "");
		password = pref.getString("password", "");
		bot = pref.getString("bot", "");
		screen_name = pref.getString("screen_name", "");
		
		UserEdit = (EditText)this.findViewById(R.id.user_edit);
		PwdEdit = (EditText)this.findViewById(R.id.pwd_edit);
		BotEdit = (EditText)this.findViewById(R.id.bot_edit);
		ScreenNameEdit = (EditText)this.findViewById(R.id.screen_name_edit);
		
		OKBtn = (Button)this.findViewById(R.id.ok_btn);
		ImportBtn = (Button)this.findViewById(R.id.import_btn);
	
		UserEdit.setText(user);
		PwdEdit.setText(password);
		BotEdit.setText(bot);
		ScreenNameEdit.setText(screen_name);
		OKBtn.setOnClickListener(this);
		ImportBtn.setOnClickListener(this);
		fromOld = false;
	}

	boolean fromOld;
	@Override
	public void onClick(View v) {
		switch (v.getId())
		{
		case R.id.ok_btn:
			user = UserEdit.getText().toString();
			password = PwdEdit.getText().toString();
			bot = BotEdit.getText().toString();
			screen_name = ScreenNameEdit.getText().toString();
			pref.edit()
				.putString("user", user)
				.putString("password",password)
				.putString("bot", bot)
				.putString("screen_name", screen_name)
				.commit();
			if (fromOld) {
				new AlertDialog.Builder(this)
					.setTitle("删除提示")
					.setMessage("旧数据已导入,删除旧配置吗?")
					.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(
									DialogInterface dialog, 
									int which) {
								finish();
							}
						})
					.setPositiveButton("删除",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(
									DialogInterface dialog,
									int which) {
								clearOldPreferences();
								finish();
							}
						})
					.show();
			}
			
			if (!fromOld)
				finish();
			break;
		case R.id.import_btn:
			getOldPreferences();
			break;
		}
		
	}
	
	private void clearOldPreferences() {
		SharedPreferences pref = this.getSharedPreferences("userdata", MODE_PRIVATE);
		pref.edit().clear().commit();
	}

	private void getOldPreferences() {
		fromOld = true;
		SharedPreferences pref = this.getSharedPreferences("userdata", MODE_PRIVATE);
		UserEdit.setText(pref.getString("user", ""));
		PwdEdit.setText(pref.getString("password", ""));
		BotEdit.setText(pref.getString("bot", ""));
		ScreenNameEdit.setText(pref.getString("screen_name", ""));
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (this.getSharedPreferences("userdata", MODE_PRIVATE)
			.getString("user", "").length() > 0)
		{
			ImportBtn.setVisibility(View.VISIBLE);
		}
	}

}
